package com.sanhuo.idms.service;

import com.sanhuo.idms.bo.RepairTaskSaveBO;
import com.sanhuo.idms.dao.RepairMapper;
import com.sanhuo.idms.dao.RepairTaskMapper;
import com.sanhuo.idms.entity.Repair;
import com.sanhuo.idms.entity.RepairPersonal;
import com.sanhuo.idms.entity.RepairTask;
import com.sanhuo.idms.enums.RepairStateEnum;
import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.security.UserVo;
import com.sanhuo.triild.Basic.BaseMapper;
import com.sanhuo.triild.Basic.BaseService;
import com.sanhuo.triild.annotation.MapperWire;
import com.sanhuo.utils.basic.BeanUtil;
import com.sanhuo.utils.basic.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RepairTaskService extends BaseService<RepairTask> {
    @MapperWire
    private RepairTaskMapper repairTaskMapper;
    @Autowired
    private RepairService repairService;
    @Autowired
    private RepairPersonalService repairPersonalService;

    @Override
    public BaseMapper<RepairTask> getBaseMapper() {
        return repairTaskMapper;
    }

    public List<String> findRepairIds(String personalId) {
        return repairTaskMapper.findRepairIds(personalId);
    }

    public void save(final UserVo user, RepairTaskSaveBO bo) {
        Repair repair = repairService.findById(bo.getRepairId());
        ExceptionUtil.throwException(BeanUtil.isNull(repair), "不存在该维修任务数据");
        repair.setState(RepairStateEnum.INPROCESSING.getCode());
        RepairPersonal repairPersonal = repairPersonalService.findById(bo.getPersonalId());
        ExceptionUtil.throwException(BeanUtil.isNull(repairPersonal), "不存在此维修人员");
        repair.updatorInfo(user);
        repairService.update(repair);
        RepairTask repairTask = new RepairTask();
        repairTask.setId(StringUtil.UUID());
        repairTask.setPersonalId(bo.getPersonalId());
        repairTask.setRepairId(bo.getRepairId());
        repairTask.creatorAndUpdatorInfo(user);
        save(repairTask);
    }
}
